using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;


namespace GameWP
{
    public class PlayConstComponent : Microsoft.Xna.Framework.DrawableGameComponent
    {
        Game _game;
        private SpriteBatch _spriteBatch;
        private Vector2 touchPosition, pretouchPosition;
        private Button _viewArrowRight, _viewArrowLeft, _viewLuffy, _Hp_ManaBar;
        private Button _pause;        
        public Button Pause
        {
            get { return _pause; }
            set { _pause = value; }
        }
        
        private float elapsedTime;
        private Vector2 _parallax;        
        public Vector2 Parallax
        {
            get { return _parallax; }
            set { _parallax = value; }
        }


        //Initilation
        public PlayConstComponent(Game game, SpriteBatch spriteBatch, List<Texture2D> ViewIcon)
            : base(game)
        {
            _game = game;
            _spriteBatch = spriteBatch;
            Parallax = Vector2.One;

            //Init button
            _viewArrowLeft = new Button(ViewIcon[1], new Vector2(300, 80)) { HaveSound = false };
            _viewLuffy = new Button(ViewIcon[2], new Vector2(370, 80)) { HaveSound = false };
            _viewArrowRight = new Button(ViewIcon[0], new Vector2(440, 80)) { HaveSound = false };
            _pause = new Button(ViewIcon[3], new Vector2(670, 10));            
            _viewArrowRight.Selected += new Button.IsSelected(_viewArrowRight_Selected);
            _viewArrowLeft.Selected += new Button.IsSelected(_viewArrowLeft_Selected);
            _viewLuffy.Selected += new Button.IsSelected(_viewLuffy_Selected);
            _Hp_ManaBar = new Button(ViewIcon[7],new Vector2(0,0)){HaveAnimation = false};
        }

        void _viewLuffy_Selected(int input)
        {
            ManagerGame.luffy.IsStateView = true;
            ManagerGame.camera.LookAt(ManagerGame.luffy.Position);
        }

        void _viewArrowLeft_Selected(int input)
        {
            ManagerGame.luffy.IsStateView = true;
            ManagerGame.camera.Move(new Vector2(-250.0f * elapsedTime, 0.0f), true);
        }

        void _viewArrowRight_Selected(int input)
        {
            ManagerGame.luffy.IsStateView = true;
            ManagerGame.camera.Move(new Vector2(250.0f * elapsedTime, 0.0f), true);
        }

        public override void Initialize()
        {
            // TODO: Add your initialization code here
                base.Initialize();
        }

        public override void Update(GameTime gameTime)
        {
            elapsedTime = (float)gameTime.ElapsedGameTime.TotalSeconds;
            // TODO: Add your update code here    
            if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed)
                _game.Exit();
            //Key input
            TouchCollection touchCollection = TouchPanel.GetState();
            touchPosition = Vector2.Zero;
            if (touchCollection.Count > 0)
            {
                touchPosition = touchCollection[0].Position;
                pretouchPosition = touchPosition;
            }

            if (!_viewArrowRight.ButtonIsTouched(touchPosition, 0)
                && !_viewArrowLeft.ButtonIsTouched(touchPosition, 0)
                && !_viewLuffy.ButtonIsTouched(touchPosition, 0))
                    ManagerGame.luffy.IsStateView = false;
            

            if (TouchPanel.IsGestureAvailable)
            {
                // Read in the Gesture input
                GestureSample gesture = TouchPanel.ReadGesture();
                // Process Gestures
                if (gesture.GestureType == GestureType.Tap)
                {
                    _pause.ButtonIsTouched(gesture.Position, 0);
                    ManagerGame.goToShop.ButtonIsTouched(gesture.Position, 0);
                    if (ManagerGame.managerTrap.IsOpen)
                    {
                        ManagerGame.managerTrap.Up.ButtonIsTouched(gesture.Position, 0);
                        for (int i = 0; i < ManagerGame.managerTrap.ListButtonTrapHave.Count; i++)
                            ManagerGame.managerTrap.ListButtonTrapHave[i].ButtonIsTouched(gesture.Position, i);
                    }
                    else
                        ManagerGame.managerTrap.Down.ButtonIsTouched(gesture.Position, 0);
                }
            }
        }

        public override void Draw(GameTime gameTime)
        {
            _spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, ManagerGame.camera.GetViewMatrix(_parallax));
                base.Draw(gameTime);
                ManagerGame.managerZombie.ManageDrawConst(_spriteBatch, gameTime);
                ManagerGame.managerAHero.ManageDrawConst(_spriteBatch, gameTime);
                ManagerGame.luffy.LHpBar.Draw(_spriteBatch, gameTime);
                ManagerGame.luffy.LManaBar.Draw(_spriteBatch, gameTime);
                ManagerGame.luffy.BalanceBar.Draw(_spriteBatch, gameTime);
                for (int i = 0; i < ManagerGame.luffy.LevelSkill; i++)
                    ManagerGame.luffy.ListButtonSkill[i].Draw(_spriteBatch, gameTime);
                _viewArrowRight.Draw(_spriteBatch, gameTime);
                _viewArrowLeft.Draw(_spriteBatch, gameTime);
                _viewLuffy.Draw(_spriteBatch, gameTime);
                _pause.Draw(_spriteBatch, gameTime);
                ManagerGame.managerTrap.ManageDrawConst(_spriteBatch, gameTime);
                ManagerGame.goToShop.Draw(_spriteBatch, gameTime);
                _Hp_ManaBar.Draw(_spriteBatch, gameTime);
                ManagerGame.luffy.LevelBar.Draw(_spriteBatch, gameTime);
            _spriteBatch.End();
        }
    }
}
